using Asteroid.Content;
using GameFramework.Extensions;
using GameFramework.GameObjects;
using GameFramework.Helpers;
using Microsoft.Xna.Framework;

namespace Asteroid.GameObjects
{
    /// <summary>
    /// This class represent a rock object.
    /// </summary>
    public class Rock : Texture
    {
        /// <summary>
        /// The rock rotation speed.
        /// </summary>
        private float rotationSpeed;

        /// <summary>
        /// Initializes a new instance of the <see cref="Rock"/> class.
        /// </summary>
        public Rock()
        {
            // Choose one of three types or rocks
            TextureName = Textures.Rocks[GameHelper.GetRandomInt(0, 2)];
         
            // Set the origin of the rock at the center of the texture
            SetOriginAtCenter();

            // Choose a random rotation speed
            rotationSpeed = GameHelper.GetRandomFloat(0, MathHelper.PiOver2);

            // Choose a random scale
            Scale = new Vector2(GameHelper.GetRandomFloat(0.3f, 1.0f));

            // Chose a random speed
            Speed = new Vector2(0, GameHelper.GetRandomFloat(10, 100));
        }

        /// <summary>
        /// Gets or sets the rock speed.
        /// </summary>
        public Vector2 Speed { get; set; }

        /// <summary>
        /// Update the rock.
        /// </summary>
        public override void Update()
        {
            base.Update();

            var t = (float)GameTime.ElapsedGameTime.TotalSeconds;

            Position += Speed * t;
            Angle += rotationSpeed * t;
        }

        /// <summary>
        /// Initialize the rock from the stream.
        /// </summary>
        /// <param name="reader">The reader.</param>
        public override void Read(System.IO.BinaryReader reader)
        {
            base.Read(reader);

            Speed = reader.ReadVector2();
            rotationSpeed = reader.ReadFloat();
        }

        /// <summary>
        /// Save the rock into the stream.
        /// </summary>
        /// <param name="writer">The writer.</param>
        public override void Write(System.IO.BinaryWriter writer)
        {
            base.Write(writer);

            writer.WriteVector2(Speed);
            writer.WriteFloat(rotationSpeed);
        }
    }
}
